/**
 * Created by 鹏 on 14-3-12.
 */
var db = require('../common/db');
var enums = require('../common/enum');

exports.myProjects = function (currentUser, callback) {
    db.execSql("Select * from Project where ProjectId in (select ProjectId from TeamMember where UserId = ?)", currentUser, function (err, data) {
        callback(data);
    });
};

exports.create = function (projectName, createUser, callback) {
    var project = {
        ProjectName: projectName,
        Status: enums.ProjectStatus.Open,
        UserId: createUser,
        CreateDate: new Date()
    };
    db.execSql("INSERT INTO Project SET ?", project, function (err, data) {
        var projectId = data.insertId;
        var member = {
            ProjectId: projectId,
            UserId: createUser
        };
        db.execSql("INSERT INTO TeamMember SET ?", member, function (err, rows) {
            if (callback != null) {
                callback();
            }
        });
    });
};

exports.get = function (projectId, callback) {
    db.execSql("Select * from Project where ProjectId = ?", projectId, function (err, data) {
        if (data.length > 0) {
            callback(data[0]);
        }
        else {
            callback(null);
        }
    });
};